package com.zcf.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.logging.LogManager;

import com.zcf.mapper.*;
import com.zcf.pojo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.zcf.common.json.Body;
import com.zcf.controller.OrderController;
import com.zcf.service.OrderService;
import com.zcf.utils.Hutool;
import com.zcf.utils.MapUtils;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zhouzhuang
 * @since 2018-11-28
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Orders> implements OrderService {
	@Autowired
	private OrderMapper orderMapper;
	@Autowired
	private OrderinfoMapper orderinfoMapper;
	@Autowired
	private MyCouponMapper myCouponMapper;
	@Autowired
	private UserMapper userMapper;
	@Autowired
	private AddressMapper addressMapper;
	@Autowired
	private RidingMapper ridingMapper;
	@Autowired
	private DiningMapper diningMapper;
	@Autowired
	private RidingOrderMapper ridingOrderMapper;
	@Autowired
	private ProductMapper productMapper;
	@Autowired
	LogMapper logMapper;

	public Body myOrder(String userId, String diningName, String productName, Double productPrice, Integer number,
			Double total, String diningId, String remark, String productId) {
		Dining dining = new Dining();
		dining.setId(diningId);
		dining = dining.selectById();
		if (dining == null) {
			return Body.newInstance("商家不存在");
		}
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String time = sdf.format(date);
		User user = new User();
		user.setId(userId);
		User one = userMapper.selectOne(user);
		if (one != null && !"".equals(one)) {
			String name = one.getName();
			List<Address> list = addressMapper.addressList(userId);
			if (!list.isEmpty()) {
				String shopAddress = list.get(0).getAddress();
				Orders order = new Orders();
				order.setId(Hutool.createId());
				order.setShopPhone(one.getPhone());
				order.setShopName(name);
				order.setDiningName(dining.getDiningName());
				order.setProductName(productName);
				order.setProductPrice(productPrice);
				order.setProductId(productId);
				order.setDiningId(diningId);
				order.setNumber(number);
				order.setTotal(total);
				order.setRemark(remark);
				order.setShopAddress(shopAddress);
				order.setTime(time);
				order.setUserId(userId);
				orderMapper.insert(order);
				Product selectById = productMapper.selectById(productId);
				selectById.setSaleNums(selectById.getSaleNums() + 1);
				productMapper.updateById(selectById);
				return Body.newInstance(order);
			}
			return Body.newInstance(501, "请添加默认地址");
		}
		return Body.newInstance(501, "用户不存在");
	}

	public Body backorder(String userId,String id){
		Orderinfo orderinfo = orderinfoMapper.selectById(id);
		String couponMoney = orderinfo.getCouponMoney();
		User user = new User();
		user.setId(userId);
		User one = userMapper.selectOne(user);
		MyCoupon myCoupon = new MyCoupon();
		myCoupon.setMoney(couponMoney);
		myCoupon.setUserId(one.getId());

		myCoupon = myCouponMapper.selectOne(myCoupon);
		if(myCoupon!=null){
			myCoupon.setUse(0);
			myCoupon.updateById();
		}
		orderinfoMapper.deleteById(id);
		
		return Body.newInstance(200,"取消成功");
	}
	@Override
	public Body addOrders(String shopPhone, String diningId, String goodslist) {

		return null;
	}

	public Body jiedan(String id, String ridingId) {
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String time = sdf.format(date);
		Orders order = new Orders();
		order.setId(id);
		Orders one = orderMapper.selectById(id);
		if (one != null && !"".equals(one)) {
			one.setDiningStatus(1);// 商家接单
			one.setShopStatus(1);// 待收货
			one.setRidingId(ridingId);
			orderMapper.updateById(one);
			Dining dining = new Dining();
			dining.setId(one.getDiningId());
			Dining din = diningMapper.selectById(dining);
			String diningAddress = din.getAddress();

			RidingOrder riding = new RidingOrder();
			riding.setId(Hutool.getId());
			riding.setOrderId(id);
			riding.setRidingId(ridingId);
			riding.setShopAddress(one.getShopAddress());
			riding.setShopName(one.getShopName());
			riding.setShopPhone(one.getShopPhone());
			riding.setProductAddress(diningAddress);
			riding.setProductPhone(din.getPhone());
			riding.setProductName(din.getDiningName());
			riding.setGetTime(time);
			ridingOrderMapper.insert(riding);
			return Body.newInstance(one);
		}
		return Body.newInstance(501, "订单不存在");
	}

	public Body shopList(String userId, String shopStatus) throws Exception {
		EntityWrapper<Orderinfo> ew = new EntityWrapper<>();
		ew.eq("user_id", userId);
		if (shopStatus != null) {
			ew.eq("status", shopStatus);
		}
		Orderinfo orderinfo = new Orderinfo();
		List<Orderinfo> list = orderinfo.selectList(ew);
		List<Orderinfo> li = new ArrayList<Orderinfo>();
		if (list.size() > 0) {
			OrderController orderController = new OrderController();
			for (Orderinfo order : list) {
				Dining dining = new Dining();
				dining.setId(order.getDinId());
				dining = dining.selectById();
				if (dining != null) {
					order.setDinImage(dining.getLogo());
					order.setDinName(dining.getDiningName());
					String time = orderController.DataFormat(order.getAddtime());
					order.setTime(time);
					order.setBox(dining.getBox() + "");
				}
				EntityWrapper<RidingOrder> ewgg = new EntityWrapper<>();
				ewgg.eq("order_id", order.getOrderInfoId());
				RidingOrder ridingOrder = new RidingOrder();
				ridingOrder = ridingOrder.selectOne(ewgg);
				if (ridingOrder != null) {
					Riding riding = new Riding();
					EntityWrapper<Riding> ewp = new EntityWrapper<>();
					ewp.eq("user_id", ridingOrder.getRidingId());
					riding = riding.selectOne(ewp);
					if (riding != null) {
						order.setQishouphone(riding.getPhone());
					}
				}
				Orders orders = new Orders();
				EntityWrapper<Orders> ewo = new EntityWrapper<>();
				ewo.eq("order_info_id", order.getOrderInfoId());
				List<Orders> liOrders = orders.selectList(ewo);
				List<Orders> lis = new ArrayList<>();
				for (Orders orderinfo2 : liOrders) {
					Product product = new Product();
					if (orderinfo2.getProductId() != null) {
						product.setId(orderinfo2.getProductId());
						product = product.selectById();
						orderinfo2.setProductName(product.getProductName());
						lis.add(orderinfo2);
					}
					continue;
				}
				order.setOrders(lis);
				if (order.getStatus() == 0) {
					order.setShopType("待付款");
					li.add(order);
				}
				if (order.getStatus() == 1) {
					order.setShopType("确认收货");
					li.add(order);
				}
				if (order.getStatus() == 2) {
					order.setShopType("待评价");
					li.add(order);
				}
				if (order.getStatus() == 3) {
					order.setShopType("退单");
					li.add(order);
				}
				if (order.getStatus() == 4) {
					order.setShopType("评价完成");
					li.add(order);
				}
				if (order.getStatus() == 5) {
					order.setShopType("拒绝接单");
					li.add(order);
				}
			}
			return Body.newInstance(li);
		}
		return Body.newInstance(501, "查无消息");
	}

	public Body diningList(String diningId) {
		List<Orders> list = orderMapper.diningList(diningId);
		ArrayList<Orders> li = new ArrayList<Orders>();
		if (!list.isEmpty()) {
			for (Orders order : list) {
				if (order.getDiningStatus() == 0) {
					order.setDiningType("等待接单");
					li.add(order);

				}
				if (order.getDiningStatus() == 1) {
					order.setDiningType("已接单");
					li.add(order);

				}
				if (order.getDiningStatus() == 2) {
					order.setDiningType("配送中");
					li.add(order);

				}
				if (order.getDiningStatus() == 3) {
					order.setDiningType("配送完成");
					li.add(order);
				}
			}
			return Body.newInstance(li);
		}
		return Body.newInstance(501, "目前没有订单信息");
	}

}
